use fluffy::{tmpl::Tpl, model::Model, db};
use actix_web::HttpResponse;
use crate::models::Videos;
use crate::caches::tags;

/// 测试
pub async fn test() -> &'static str { 
    "hello, there"
}

/// 默认首页
pub async fn index(tpl: Tpl) -> HttpResponse { 
    let mut data = tmpl_data![];
    let query = query![
        fields => "id, title, remark, created, tag_ids, cover_image",
        order_by => "seq DESC",
        limit => 10,
    ];
    let cond = cond![
        "is_recommended" => "1",
    ];
    let mut conn = db::get_conn();
    let result = Videos::fetch_rows(&mut conn, &query, Some(&cond));
    let mut rows = vec![];
    for r in result { 
        let (id, title, remark, created, tag_ids, cover_image): (usize, String, String, u32, String, String) = from_row!(r);
        rows.push(Videos{id, title, remark, created, tag_ids: tags::get_tag_links(tag_ids), cover_image});
    }
    data.insert("videos", &rows);
    data.insert("item_count", &rows.len());
    super::set_navs(&mut data);
    data.insert("nav_title", "");
    render!(tpl, "index/index.html", &data)
}

/// 关于我们
pub async fn about(tpl: Tpl) -> HttpResponse { 
    let mut data = tmpl_data![];
    super::set_navs(&mut data);
    data.insert("nav_title", "关于我们");
    render!(tpl, "index/about.html", &data)
}

/// 联系我们
pub async fn contact(tpl: Tpl) -> HttpResponse { 
    let mut data = tmpl_data![];
    super::set_navs(&mut data);
    data.insert("nav_title", "联系我们");
    render!(tpl, "index/contact.html", &data)
}

/// 留言反馈
pub async fn feedback(tpl: Tpl) -> HttpResponse { 
    let mut data = tmpl_data![];
    super::set_navs(&mut data);
    data.insert("nav_title", "留言反馈");
    render!(tpl, "index/feedback.html", &data)
}
